var compare_retail_tax_or_cost_up_with_discount_amount = JSON.parse('{"CODE":0, "ACTION":"","LEVEL":"T1", "MSG":"","DATA":"","SCRIPTNAME":"compare_retail_tax_or_cost_up_with_discount_amount"}');

if (active_discount == 1) {
    var compare_retail_tax_or_cost_up_with_discount_amountClubList = []
    if (!isNull(item_store_json)) {
        var itemStoreJsonList = JSON.parse(item_store_json);
        itemStoreJsonList.forEach(function (itemStoreJson) {
            if (!isNull(trait_nbr) && trait_nbr == 'ALL') {
                if (discount_range === 'price') {
                    var priceDiscountSubtract = func_active_price_tax(itemStoreJson.price_action_comp_retail, itemStoreJson.price_action_pc_retail, itemStoreJson.unit_retail_amt, iva_tax) - retail_tax
                    if (!isNull(retail_tax) && (priceDiscountSubtract < 0 || (priceDiscountSubtract - discount_amount < 0))) {
                        compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                    }
                } else if (discount_range === 'cost') {
                    var costDiscountSubtract = func_cost_by_price_action_pc_cost_vnpk_cost(itemStoreJson.price_action_pc_cost, itemStoreJson.vnpk_cost_amt) - cost
                    var itemStoreVnpkQty = db2_vnpk_qty;
                    if (isNull(itemStoreVnpkQty) || itemStoreVnpkQty == 0) {
                        itemStoreVnpkQty = 1
                    }
                    var costDiscountDivide = parseFloat((costDiscountSubtract / itemStoreVnpkQty).toFixed(2));
                    if (!isNull(cost) && (costDiscountSubtract < 0 || costDiscountDivide < discount_amount)) {
                        compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                    }
                }
            } else if (!isNull(trait_nbr)) {
                if (!isNull(trait_store) && trait_store.containsKey(trait_nbr)) {
                    var clubList = trait_store.get(trait_nbr)
                    if (!isNull(clubList)) {
                        clubList.forEach(function (club) {
                            if (club == itemStoreJson.store_id) {
                                if (discount_range === 'price') {
                                    var priceDiscountSubtract = func_active_price_tax(itemStoreJson.price_action_comp_retail, itemStoreJson.price_action_pc_retail, itemStoreJson.unit_retail_amt, iva_tax) - retail_tax
                                    if (!isNull(retail_tax) && (priceDiscountSubtract < 0 || (priceDiscountSubtract - discount_amount < 0))) {
                                        compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                                    }
                                } else if (discount_range === 'cost') {
                                    var costDiscountSubtract = func_cost_by_price_action_pc_cost_vnpk_cost(itemStoreJson.price_action_pc_cost, itemStoreJson.vnpk_cost_amt) - cost
                                    var itemStoreVnpkQty = db2_vnpk_qty;
                                    if (isNull(itemStoreVnpkQty) || itemStoreVnpkQty == 0) {
                                        itemStoreVnpkQty = 1
                                    }
                                    var costDiscountDivide = parseFloat((costDiscountSubtract / itemStoreVnpkQty).toFixed(2));
                                    if (!isNull(cost) && (costDiscountSubtract < 0 || costDiscountDivide < discount_amount)) {
                                        compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                                    }
                                }
                            }
                        })
                    }
                }
            } else if (!isNull(store_id) && store_type == 1) {
                if (itemStoreJson.store_id == store_id) {
                    if (discount_range === 'price') {
                        var priceDiscountSubtract = func_active_price_tax(itemStoreJson.price_action_comp_retail, itemStoreJson.price_action_pc_retail, itemStoreJson.unit_retail_amt, iva_tax) - retail_tax
                        if (!isNull(retail_tax) && (priceDiscountSubtract < 0 || (priceDiscountSubtract - discount_amount < 0))) {
                            compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                        }
                    } else if (discount_range === 'cost') {
                        var costDiscountSubtract = func_cost_by_price_action_pc_cost_vnpk_cost(itemStoreJson.price_action_pc_cost, itemStoreJson.vnpk_cost_amt) - cost
                        var itemStoreVnpkQty = db2_vnpk_qty;
                        if (isNull(itemStoreVnpkQty) || itemStoreVnpkQty == 0) {
                            itemStoreVnpkQty = 1
                        }
                        var costDiscountDivide = parseFloat((costDiscountSubtract / itemStoreVnpkQty).toFixed(2));
                        if (!isNull(cost) && (costDiscountSubtract < 0 || costDiscountDivide < discount_amount)) {
                            compare_retail_tax_or_cost_up_with_discount_amountClubList.push(itemStoreJson.store_id)
                        }
                    }
                }
            }
        })
    }
    if (!isNull(item_dc_json)) {
        var itemDcJsonObj = JSON.parse(item_dc_json);
        itemDcJsonObj.forEach(function (idj) {
            if (!isNull(trait_nbr) && trait_nbr == 'ALL') {
                if (discount_range === 'cost') {
                    var costDiscountSubtract = func_cost_by_price_action_pc_cost_vnpk_cost(idj.price_action_pc_cost, idj.vnpk_cost_amt) - cost
                    var itemStoreVnpkQty = db2_vnpk_qty;
                    if (isNull(itemStoreVnpkQty) || itemStoreVnpkQty == 0) {
                        itemStoreVnpkQty = 1
                    }
                    var costDiscountDivide = parseFloat((costDiscountSubtract / itemStoreVnpkQty).toFixed(2));
                    if (!isNull(cost) && (costDiscountSubtract < 0 || costDiscountDivide < discount_amount)) {
                        compare_retail_tax_or_cost_up_with_discount_amountClubList.push(idj.dc_id + '-仓')
                    }
                }
            } else if (!isNull(store_id) && store_type == 2) {
                if (idj.dc_id == store_id) {
                    if (discount_range === 'cost') {
                        var costDiscountSubtract = idj.vnpk_cost_amt - cost
                        var itemStoreVnpkQty = db2_vnpk_qty;
                        if (isNull(itemStoreVnpkQty) || itemStoreVnpkQty == 0) {
                            itemStoreVnpkQty = 1
                        }
                        var costDiscountDivide = parseFloat((costDiscountSubtract / itemStoreVnpkQty).toFixed(2));
                        if (!isNull(cost) && (costDiscountSubtract < 0 || costDiscountDivide < discount_amount)) {
                            compare_retail_tax_or_cost_up_with_discount_amountClubList.push(idj.dc_id + '-仓')
                        }
                    }
                }
            }
        })
    }
    if (compare_retail_tax_or_cost_up_with_discount_amountClubList.length > 0) {
        compare_retail_tax_or_cost_up_with_discount_amount.CODE = 2;
        compare_retail_tax_or_cost_up_with_discount_amount.ACTION = 'error';
        compare_retail_tax_or_cost_up_with_discount_amount.MSG = '门店[' + compare_retail_tax_or_cost_up_with_discount_amountClubList.join(',') + ']补差金额要小于价差';
    }
}

JSON.stringify(compare_retail_tax_or_cost_up_with_discount_amount);